<template>
  <div>
    <el-card>
      <template #header>
        <h4>商品添加</h4>
      </template>
      <div>
        <el-form ref="form" :model="comForm" label-width="80px">
          <el-form-item label="商品名称">
            <el-input v-model="comForm.name" class="ww"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-select v-model="comForm.category" placeholder="请选择商品分类">
              <el-option
                v-for="item in cateList"
                :key="item.id"
                :label="item.cateName"
                :value="item.cateName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品价格">
            <el-input-number
              v-model="comForm.price"
              @change="handleChange"
              :min="0.1"
              :max="99999999"
              label="描述文字"
            ></el-input-number>
          </el-form-item>

          <el-form-item label="商品图片">
            <el-upload
              class="avatar-uploader"
              action="http://172.16.100.203:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="商品描述">
            <el-input
              class="ww"
              type="textarea"
              v-model="comForm.goodsDesc"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">添加商品</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { comAdd, getcate } from '../../api/info.js'
export default {
  data() {
    return {
      comForm: {
        name: '',
        category: '',
        price: 1,
        imgUrl: '',
        goodsDesc: ''
      },
      imageUrl: '',
      cateList: []
    }
  },
  created() {
    this.getcate()
  },
  methods: {
    // 获取分类
    async getcate() {
      const { categories } = await getcate({
        currentPage: 1,
        pageSize: 999
      })
      console.log(categories)
      this.cateList = categories
    },

    async onSubmit() {
      if (
        this.comForm.name &&
        this.comForm.category &&
        this.comForm.imgUrl &&
        this.comForm.goodsDesc
      ) {
        const { code } = await comAdd(this.comForm)
        if (code === 0) {
          this.$router.push('/pro/list')
        } else {
          this.$message.error('添加失败')
        }
      } else {
        this.$message.error('请将商品信息填写完整')
      }
    },

    // 计数器
    handleChange(value) {
      this.comForm.price = value
    },

    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.comForm.imgUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}
.avatar {
  width: 140px;
  height: 140px;
  display: block;
}

.ww {
  width: 400px;
}
</style>
